package com.ctesi.service.zhaobiao.hubei;


import com.ctesi.utils.PutUtil;
import com.ctesi.utils.StringExtract;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.processor.PageProcessor;

import java.time.LocalDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ctesi.utils.StringExtract.*;


//本类用于爬取所有页面中的招标url和相应url下的内容。
@Service
public class HuBeiProcessorService implements PageProcessor {

    @Override
    public void process(Page page) {

        //通过字符串比对正则表达式的方式处理对应的网页逻辑，展示当前的网页地址。
        String regex = "http://www\\.hbbidcloud\\.cn/hubei/jyxx/004002/.*-.*\\.html";
        String url1 = page.getUrl().get();
        System.out.println("当前的爬取的url为：" + url1);


        //url裂变逻辑
        if (!page.getUrl().get().matches(regex)) {
            System.out.println("-----------------正在对url进行裂变-------------------");

            // 可能会抛出异常的代码,有些页面的招标链接不足19条，一般是最后一页，所以使用太try。
            for (int i1 = 1; i1 <= 19; i1++) {

                //爬取所有页面的招标公告链接
                try {

                    //如果连接中含有about的话，表示是起始页面，将爬取所有招标公告的链接
                    String url = "http://www.hbbidcloud.cn" + page.getHtml().xpath(String.format("/html/body/div[2]/div[2]/div[2]/div/div[2]/ul/li[%d]/div/a/@href", i1)).toString();
                    //判断招标公告的发布时间是否是2023年，如果是，则爬取该招标公告。
                    String biddingPubTime = page.getHtml().xpath(String.format("/html/body/div[2]/div[2]/div[2]/div/div[2]/ul/li[%d]/span/text()", i1)).toString();
                    if (biddingPubTime.contains("2023")) {
                        page.addTargetRequest(url); //添加到待爬网址中
//                        System.out.println("添加" + url + "成功！");
                    } else {
                        System.out.println("招标公告非2023年，舍去。" + i1);
                    }


                } catch (Exception e) {
                    System.out.println("-------------全部招标链接爬取完毕，当前页面共爬取" + i1 + "条招标公告-----------");
                    break;
                }


            }


        }

        //内容抓取逻辑
        if (page.getUrl().get().matches(regex)) {
            //内容页面，直接抓取对应的内容。
            System.out.println("------------------开始爬取内容-----------");
            StringExtract stringExtract = new StringExtract();

            //当前爬取的url
            page.putField("bidUrl",page.getUrl().get());

            //招标公告所属省份 - 湖北省
            page.putField("bidProvince","湖北省");

            //获取招标内容主体
            String context = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody").get();
            String context1 = removeTags(context);
            String context2 = StringExtract.removeEmptyLines(context1);
            String bidContext = removeTags(removeSpaces(context2));
            System.out.println(bidContext);

            //招标标题
            String bidTitle = removeSpaces(getFirstLine(bidContext));
            page.putField("bidTitle", bidTitle);

            //招标文件截止时间
            PutUtil.putField(page,bidContext,"投标文件递交截止时间为：(.*?)分","deadLine");

            //合同估算价
            PutUtil.putField(page,bidContext,"合同估算价：(.*?)元\\n","bidPrice");

            //招标条件
            PutUtil.putField(page,bidContext,"招标条件\\n(.*?)。\\n","bidCondition");


            //招标省份
            String pattern = "【(.*?)】";
            Pattern province = Pattern.compile(pattern);
            Matcher matcher = province.matcher(page.getHtml().xpath("/html/body/div[2]/div[2]/div/h3/text()").toString());
            if (matcher.find()) {
                String bidArea = matcher.group(1);
                page.putField("bidArea", bidArea);
            }

            //投标人资格要求
            String creditPattern = "投标人资格要求\\n(.*?)\\n.*?\\.招标文件的获取";
            Pattern regexPattern1 = Pattern.compile(creditPattern, Pattern.DOTALL);
            Matcher matcher2 = regexPattern1.matcher(bidContext);
            if (matcher2.find()) {
                String bidCredit = matcher2.group(1);
                page.putField("bidCredit",bidCredit);
                System.out.println(bidCredit);
            }


            //评标办法
            String methodPattern = "7\\.评标办法\\n(.*?)\\n8\\.发布公告的媒介"; // 匹配要提取内容的正则表达式
            Pattern regexPattern2 = Pattern.compile(methodPattern, Pattern.DOTALL);
            Matcher matcher3 = regexPattern2.matcher(bidContext);
            if (matcher3.find()) {
                String bidJudgeMethod1 = matcher3.group(1);
                String bidJudgeMethod = removeNbsp(bidJudgeMethod1);
                page.putField("bidJudgeMethod",bidJudgeMethod);
                System.out.println(bidJudgeMethod);
            }


            //招标状态
            String bidStatus = page.getHtml().xpath("/html/body/div[2]/div[2]/div/h3/font/text()").toString();
            page.putField("bidStatus", bidStatus);

            //招标范围
            String pattern1 = "招标范围：([^\n]*)\n";
            Pattern regex1 = Pattern.compile(pattern1);
            Matcher matcher1 = regex1.matcher(bidContext);
            if (matcher1.find()) {
                String bidArange = matcher1.group(1);
                page.putField("bidArange", bidArange);
            } else {
                System.out.println("没有找到招标范围");
            }

            //招标发布时间
            String timePattern = "账号:\\n(.*?)\\n备注:"; // 匹配要提取的内容的正则表达式
            Pattern regexPattern4 = Pattern.compile(timePattern);
            Matcher matcher4 = regexPattern4.matcher(bidContext);
            if (matcher4.find()) {
                String bidPubTime = matcher4.group(1);
                page.putField("bidPubTime",bidPubTime);
                System.out.println("招标发布时间：" + bidPubTime);
            }else {
                System.out.println("没有爬取到招标发布时间");
            }

            //招标编号
            String bidId = extractTenderNumber(bidContext);
            page.putField("bidId", bidId);


            //招标人
            String bidUnity = getTenderer(bidContext);
            page.putField("bidUnity", bidUnity);


            //招标地址
            String bidAddress = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[2]/td[2]/div/text()").toString();
            page.putField("bidAddress", bidAddress);


            //招标联系人姓名
            String bidName = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[4]/td[2]/div/text()").toString();
            page.putField("bidName", bidName);

            //招标联系人电话
            String bidPhone = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[5]/td[2]/div/text()").toString();
            page.putField("bidPhone", bidPhone);


            //备注
            String memo = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[27]/td/text()").toString();
            page.putField("memo", memo);


            //代理机构名称
            String agencyUnity = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[1]/td[4]/div/text()").toString();
            page.putField("agencyUnity", agencyUnity);


            //代理机构地址
            String agencyAddress = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[2]/td[4]/div/text()").toString();
            page.putField("agencyAddress", agencyAddress);


            //代理机构联系人名字
            String agencyName = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[4]/td[4]/div/text()").toString();
            page.putField("agencyName", agencyName);


            //代理机构联系人电话
            String agencyPhone = page.getHtml().xpath("/html/body/div[2]/div[2]/div/div[2]/div/table/tbody/tr[24]/td/table/tbody/tr[5]/td[4]/div/text()").toString();
            page.putField("agencyPhone", agencyPhone);


            //数据创建时间
            LocalDateTime createTime = LocalDateTime.now();
            page.putField("createTime", createTime);
            System.out.println("数据创建时间：" + createTime);


            //数据更新时间
            LocalDateTime updateTime = LocalDateTime.now();
            page.putField("updateTime", updateTime);
            System.out.println("数据更新时间：" + updateTime);

            //招标类型
            String currentUrl = page.getUrl().toString();
            if (currentUrl.contains("004002001")){
                page.putField("bidType", "房建市政");
            } else if (currentUrl.contains("004002002")) {
                page.putField("bidType", "水利工程");
            } else if (currentUrl.contains("004002003")) {
                page.putField("bidType", "交通工程");
            }


        }


    }


}







